﻿#include "viewer.h"

QMap<int, QString> direct2literal{
    {UP, QStringLiteral("上")},
    {DOWN, QStringLiteral("仰")},
    {FORWARD, QStringLiteral("前")},
    {BACK, QStringLiteral("后")},
    {LEFT, QStringLiteral("左")},
    {RIGHT, QStringLiteral("右")},
    {UPFORWARD, QStringLiteral("上前")},
    {UPBACK, QStringLiteral("上后")},
    {UPLEFT, QStringLiteral("上左")},
    {UPRIGHT, QStringLiteral("上右")},
    {DOWNFORWARD, QStringLiteral("仰前")},
    {DOWNBACK, QStringLiteral("仰后")},
    {DOWNLEFT, QStringLiteral("仰左")},
    {DOWNRIGHT, QStringLiteral("仰右")},
    {ORIGIN, QStringLiteral("")}
};

void Viewer::initLeds()
{
    for(int i=1;i<=10;++i){
        if(this->vecOpLeds[i]){
            this->vecOpLeds[i]->resetStatus();
            continue;
        }
        QObject* op = qmlRootObj->findChild<QObject*>("op-"+QString::number(i));
        this->vecOpLeds[i]=new Led(op);
    }

    for(int i=1;i<=6;++i){
        for(int j=1;j<=4;++j){
            if(this->vecScrewLeds[i][j]){
                this->vecScrewLeds[i][j]->resetStatus();
                continue;
            }
            QObject* screw = qmlRootObj->findChild<QObject*>("screw-"+QString::number(i)+"-"+QString::number(j));
            this->vecScrewLeds[i][j]=new Led(screw,200);
        }
    }
}

void Viewer::ledSelfCheck(QEventLoop* loop)
{
    // 自检
    for(int i=1;i<=10+1;++i){
        QTimer::singleShot(1000+i*400,nullptr,[this,i,loop](){
            if(i>1) this->vecOpLeds[i-1]->setStatus(LEDSTATES::ON);
            if(i<=10) this->vecOpLeds[i]->setStatus(LEDSTATES::BLINK);
        });
    }
    for(int i=1;i<=6;++i){
        for(int j=1;j<=4+1;++j){
            QTimer::singleShot(1000+((i-1)*4+j)*300,nullptr,[this, i, j,loop](){
                if(j>1) this->vecScrewLeds[i][j-1]->setStatus(LEDSTATES::ON);
                if(j<=4) this->vecScrewLeds[i][j]->setStatus(LEDSTATES::BLINK);
                if(i == 6 && j == 5){
                    this->initLeds();
                    QThread::msleep(1000);
                    loop->quit();
                }
            });
        }
    }
}

Viewer::~Viewer()
{

}

void Viewer::initVideo()
{

}

// 开启摄像头
void Viewer::openCamera()
{
    
}

// 方向盘
void Viewer::initPanel()
{
    // 垂直
    if(!vecArrows[1]) vecArrows[1] = qmlRootObj->findChild<PaintItem*>("imageUp");
    if(!vecArrows[2]) vecArrows[2] = qmlRootObj->findChild<PaintItem*>("imageDown");
    // 水平
    if(!vecArrows[3]) vecArrows[3] = qmlRootObj->findChild<PaintItem*>("imageForward");
    if(!vecArrows[4]) vecArrows[4] = qmlRootObj->findChild<PaintItem*>("imageBack");
    if(!vecArrows[5]) vecArrows[5] = qmlRootObj->findChild<PaintItem*>("imageLeft");
    if(!vecArrows[6]) vecArrows[6] = qmlRootObj->findChild<PaintItem*>("imageRight");
    // 设置垂直图片
    vecArrows[1]->updateImage(QImage(":/png/blue_up.png"));
    vecArrows[2]->updateImage(QImage(":/png/blue_down.png"));
    // 设置水平图片
    vecArrows[3]->updateImage(QImage(":/png/blue_forward.png"));
    vecArrows[4]->updateImage(QImage(":/png/blue_back.png"));
    vecArrows[5]->updateImage(QImage(":/png/blue_left.png"));
    vecArrows[6]->updateImage(QImage(":/png/blue_right.png"));

    // 标志字
    if(!literal) literal = qmlRootObj->findChild<QObject*>("textPanel");
    // 设置字体颜色
    literal->setProperty("color", "#386487");
}

void Viewer::bindPanelState(DIRECT &direct)
{
    QTimer* timer = new QTimer();
    initPanel();
    static DIRECT predirect = ORIGIN;
    connect(timer, &QTimer::timeout, this, std::bind(&Viewer::updatePanelState, this, &direct,&predirect));
    timer->setInterval(10);
    timer->start();
}

void Viewer::updatePanelState(DIRECT* direct,DIRECT* predirect)
{
    if(*direct != *predirect){
        //还原predirect操作
        this->literal->setProperty("text", direct2literal[*predirect]);
        this->literal->setProperty("color","#386487");
        switch(*predirect)
        {
            case DIRECT::ORIGIN:
            {
                break;
            }
            case DIRECT::FORWARD:
            {
                this->vecArrows[FORWARD]->updateImage(QImage(":/png/blue_forward.png"));
                break;
            }
            case DIRECT::BACK:
            {
                this->vecArrows[BACK]->updateImage(QImage(":/png/blue_back.png"));
                break;
            }
            case DIRECT::LEFT:
            {
                this->vecArrows[LEFT]->updateImage(QImage(":/png/blue_left.png"));
                break;
            }
            case DIRECT::RIGHT:
            {
                this->vecArrows[RIGHT]->updateImage(QImage(":/png/blue_right.png"));
                break;
            }
            case DIRECT::UP:
            {
                this->vecArrows[UP]->updateImage(QImage(":/png/blue_up.png"));
                break;
            }
            case DIRECT::DOWN:
            {
                this->vecArrows[DOWN]->updateImage(QImage(":/png/blue_down.png"));
                break;
            }
            case DIRECT::UPFORWARD:
            {
                this->vecArrows[UP]->updateImage(QImage(":/png/blue_up.png"));
                this->vecArrows[FORWARD]->updateImage(QImage(":/png/blue_forward.png"));
                break;
            }
            case DIRECT::UPBACK:
            {
                this->vecArrows[UP]->updateImage(QImage(":/png/blue_up.png"));
                this->vecArrows[BACK]->updateImage(QImage(":/png/blue_back.png"));
                break;
            }
            case DIRECT::UPLEFT:
            {
                this->vecArrows[UP]->updateImage(QImage(":/png/blue_up.png"));
                this->vecArrows[LEFT]->updateImage(QImage(":/png/blue_left.png"));
                break;
            }
            case DIRECT::UPRIGHT:
            {
                this->vecArrows[UP]->updateImage(QImage(":/png/blue_up.png"));
                this->vecArrows[RIGHT]->updateImage(QImage(":/png/blue_right.png"));
                break;
            }
            case DIRECT::DOWNFORWARD:
            {
                this->vecArrows[DOWN]->updateImage(QImage(":/png/blue_down.png"));
                this->vecArrows[FORWARD]->updateImage(QImage(":/png/blue_forward.png"));
                break;
            }
            case DIRECT::DOWNBACK:
            {
                this->vecArrows[DOWN]->updateImage(QImage(":/png/blue_down.png"));
                this->vecArrows[BACK]->updateImage(QImage(":/png/blue_back.png"));
                break;
            }
            case DIRECT::DOWNLEFT:
            {
                this->vecArrows[DOWN]->updateImage(QImage(":/png/blue_down.png"));
                this->vecArrows[LEFT]->updateImage(QImage(":/png/blue_left.png"));
                break;
            }
            case DIRECT::DOWNRIGHT:
            {
                this->vecArrows[DOWN]->updateImage(QImage(":/png/blue_down.png"));
                this->vecArrows[RIGHT]->updateImage(QImage(":/png/blue_right.png"));
                break;
            }
        }

        //更新direct操作
        this->literal->setProperty("text", direct2literal[*direct]);
        this->literal->setProperty("color","red");
        switch(*direct)
        {
            case DIRECT::ORIGIN:
            {
                break;
            }
            case DIRECT::FORWARD:
            {
                this->vecArrows[FORWARD]->updateImage(QImage(":/png/red_forward.png"));
                break;
            }
            case DIRECT::BACK:
            {
                this->vecArrows[BACK]->updateImage(QImage(":/png/red_back.png"));
                break;
            }
            case DIRECT::LEFT:
            {
                this->vecArrows[LEFT]->updateImage(QImage(":/png/red_left.png"));
                break;
            }
            case DIRECT::RIGHT:
            {
                this->vecArrows[RIGHT]->updateImage(QImage(":/png/red_right.png"));
                break;
            }
            case DIRECT::UP:
            {
                this->vecArrows[UP]->updateImage(QImage(":/png/red_up.png"));
                break;
            }
            case DIRECT::DOWN:
            {
                this->vecArrows[DOWN]->updateImage(QImage(":/png/red_down.png"));
                break;
            }
            case DIRECT::UPFORWARD:
            {
                this->vecArrows[UP]->updateImage(QImage(":/png/red_up.png"));
                this->vecArrows[FORWARD]->updateImage(QImage(":/png/red_forward.png"));
                break;
            }
            case DIRECT::UPBACK:
            {
                this->vecArrows[UP]->updateImage(QImage(":/png/red_up.png"));
                this->vecArrows[BACK]->updateImage(QImage(":/png/red_back.png"));
                break;
            }
            case DIRECT::UPLEFT:
            {
                this->vecArrows[UP]->updateImage(QImage(":/png/red_up.png"));
                this->vecArrows[LEFT]->updateImage(QImage(":/png/red_left.png"));
                break;
            }
            case DIRECT::UPRIGHT:
            {
                this->vecArrows[UP]->updateImage(QImage(":/png/red_up.png"));
                this->vecArrows[RIGHT]->updateImage(QImage(":/png/red_right.png"));
                break;
            }
            case DIRECT::DOWNFORWARD:
            {
                this->vecArrows[DOWN]->updateImage(QImage(":/png/red_down.png"));
                this->vecArrows[FORWARD]->updateImage(QImage(":/png/red_forward.png"));
                break;
            }
            case DIRECT::DOWNBACK:
            {
                this->vecArrows[DOWN]->updateImage(QImage(":/png/red_down.png"));
                this->vecArrows[BACK]->updateImage(QImage(":/png/red_back.png"));
                break;
            }
            case DIRECT::DOWNLEFT:
            {
                this->vecArrows[DOWN]->updateImage(QImage(":/png/red_down.png"));
                this->vecArrows[LEFT]->updateImage(QImage(":/png/red_left.png"));
                break;
            }
            case DIRECT::DOWNRIGHT:
            {
                this->vecArrows[DOWN]->updateImage(QImage(":/png/red_down.png"));
                this->vecArrows[RIGHT]->updateImage(QImage(":/png/red_right.png"));
                break;
            }
        }
        *predirect = *direct;
    }

}

void Viewer::initStatusBar()
{
    for(int i = 1; i < 7; ++i){
        if(!vecStatus[i].first) vecStatus[i].first = qmlRootObj->findChild<QQuickItem*>("rectangleS" + QString::number(i));
        if(!vecStatus[i].second) vecStatus[i].second = qmlRootObj->findChild<QQuickItem*>("textS" + QString::number(i));
    }

}

void Viewer::setStatusLight(int which)
{
    this->vecStatus[which].second->setProperty("color", "red");
}

void Viewer::setStatusDark(int which)
{
    this->vecStatus[which].second->setProperty("color", "#386487");
}

void Viewer::switchPattern(bool manualPattern)
{
    if(manualPattern) this->vecStatus[STATUS::PAT].second->setProperty("text", QStringLiteral("手动"));
    else this->vecStatus[STATUS::PAT].second->setProperty("text", QStringLiteral("自动"));
}
